package com.hemaapp.yjnh.activity;

import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.utils.ImageUtils;
import com.hemaapp.yjnh.view.ShareDialog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.media.UMImage;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class QRCordActivity extends BaseActivity {

	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;
	private ImageView ivQR;
	private TextView tv_share;
	
	private User user;
	private String codeurl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_qrcode);
		super.onCreate(savedInstanceState);
		user = getApplicationContext().getUser();
		if (user != null) {
            ImageLoader.getInstance().displayImage(user.getCodeurl(), ivQR, ImageUtils.smallImageOptions);
			codeurl = user.getCodeurl();
		}else {
			Intent itLogin = new Intent(mContext, LoginActivity.class);
			startActivity(itLogin);
		}
	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);
		ivQR = (ImageView) findViewById(R.id.iv_qr);
		tv_share = (TextView) findViewById(R.id.tv_share);
	}
	
	@Override
	protected void setListener() {
		titleLeft.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		tv_share.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String content = getApplicationContext().getSysInitInfo().getMsg_invite();
//				String path = "http://123.57.210.14/index.php/user/register.html?username="+user.getUsername();//分享的跳转链接
				String path = getApplicationContext().getSysInitInfo().getSys_plugins()+"share/sdk.php?id=0&username="+user.getUsername();
				//图片url
				UMImage image = null;
				String imgUrl = user.getCodeurl();
				if (!isNull(imgUrl)) {
					image = new UMImage(mContext, imgUrl);
				}else {
					imgUrl = BaseUtil.getLogoImagePath(mContext);
					image = new UMImage(mContext, BitmapFactory.decodeFile(imgUrl));
				}
				new ShareDialog(QRCordActivity.this, "大家一起注册易家农户" , content, path, image)
						.builder().show();
			}
		});
		titleText.setText("赚粉二维码");
		titleRight.setVisibility(View.GONE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		UMShareAPI.get( this ).onActivityResult( requestCode, resultCode, data);
	}
}
